/*
 * 名称：OriginalOrderController.js
 * 时间：2017/9/16
 * 作者: sce
 * 功能:
 */
import ManualOrderModel from './../../../model/logistics/original/ManualOrderModel'
import ManualOrderDetailModel from './../../../model/logistics/original/ManualOrderDetailModel'
import BasicController from './../../BasicController'
import ErrorHandlerCode from './../../../util/ErrorHandlerCode'

class OriginalOrderController extends BasicController{
    getById(req, res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        let result = ManualOrderModel.findById(req.body.id);
        if(!result){
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw new Error('OriginalOrderController getById');
            return;
        }
        const rows = ManualOrderDetailModel.findByOrderId(req.body.id);
        result.rows = rows;
        super.RequestSuccessMsg(res, result);
    }
    insert(req, res){
        const orderHeader = req.body.order_header;
        let orderDetail = req.body.order_detail;
        const manualOrder = ManualOrderModel.insert(orderHeader);
        if(!manualOrder.id){
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            return;
        }
        const order_id = manualOrder.id;
        for(let i = 0, len = orderDetail.length; i < len ; i++){
            orderDetail[i].original_order_id = order_id;
        }
        ManualOrderDetailModel.insert(orderDetail).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    update(req, res){
        const orderHeader = req.body.order_header;
        const orderDetail = req.body['order_detail'];
        if(!super.isEmptyParameter(orderHeader.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: '未携带表单ID'
            })
            return;
        }
        ManualOrderModel.update(orderHeader).then((data) => {
        }).then(() => {
            ManualOrderDetailModel.deleteByOrderId(orderHeader.id).then((data) => {
                return data;
            }).then((result) => {
                ManualOrderDetailModel.insert(orderDetail).then((data) => {
                    return data;
                })
            }).then(() => {
                super.RequestSuccessMsg(res);
            }).catch((err) => {
                res.json(ErrorHandlerCode.INNERSERVICERROR);
                throw err;
            })
        })
    }
    deleteById(res, req){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        const id = req.body.id;
        ManualOrderDetailModel.deleteByOrderId(id).then((data) => {
            return data;
        }).then((data) => {
            ManualOrderModel.deleteRecord(id).then((result) => {
                return result;
            })
        }).then(() => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    isVerify(res, req){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        ManualOrderModel.isVerify(req.body.id).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    unVerify(res, req){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
        }
        ManualOrderModel.unVerify(req.body.id).then((data) => {
            super.RequestSuccessMsg(res);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}

module.exports = new OriginalOrderController();